/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package digitaldreamers.games.papermonsters.clients.gameclient.gameui.gamescreens;

import digitaldreamers.games.papermonsters.clients.gameclient.configurations.Configurations;
import digitaldreamers.games.papermonsters.clients.gameclient.configurations.Messages;
import digitaldreamers.games.papermonsters.clients.gameclient.gameui.PaperMonsters;
import digitaldreamers.games.papermonsters.clients.gameclient.network.packets.receiving.RLoginRequestResponse;
import digitaldreamers.games.papermonsters.clients.gameclient.network.packets.sending.SLoginRequest;
import digitaldreamers.libraries.games.clients.gameclient.gameui.implementation.RenderPanel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.UnknownHostException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

/**
 *
 * @author PrimeLabo
 */
public class LoginScreen extends AbNetworkScreen implements ActionListener {

    private static final int SPACE_GAP = 10;
    
    private static final int LBL_USERNAME_WIDTH = 75;
    private static final int LBL_USERNAME_HEIGHT = 25;
    
    private static final int LBL_PASSWORD_WIDTH = LBL_USERNAME_WIDTH;
    private static final int LBL_PASSWORD_HEIGHT = LBL_USERNAME_HEIGHT;
    
    private static final int TXT_USERNAME_WIDTH = 150;
    private static final int TXT_USERNAME_HEIGHT = 25;
    
    private static final int TXT_PASSWORD_WIDTH = TXT_USERNAME_WIDTH;
    private static final int TXT_PASSWORD_HEIGHT = TXT_USERNAME_HEIGHT;
    
    private static final int BTN_LOGIN_WIDTH = 70;
    private static final int BTN_LOGIN_HEIGHT = 25;
    
    private static final int BTN_EXIT_WIDTH = BTN_LOGIN_WIDTH;
    private static final int BTN_EXIT_HEIGHT = BTN_LOGIN_HEIGHT;
    
    private static final int LBL_ERROR_WIDTH = PaperMonsters.DEFAULT_WIDTH;
    private static final int LBL_ERROR_HEIGHT = 25;
    
    private static final int LBL_USERNAME_TOP = 327;
    private static final int LBL_USERNAME_LEFT = (PaperMonsters.DEFAULT_WIDTH - LBL_USERNAME_WIDTH - TXT_USERNAME_WIDTH - SPACE_GAP) / 2;
    
    private static final int LBL_PASSWORD_TOP = LBL_USERNAME_TOP + LBL_USERNAME_HEIGHT + SPACE_GAP;
    private static final int LBL_PASSWORD_LEFT = LBL_USERNAME_LEFT;
    
    private static final int TXT_USER_TOP = LBL_USERNAME_TOP;
    private static final int TXT_USER_LEFT = LBL_USERNAME_LEFT + LBL_USERNAME_WIDTH + SPACE_GAP;
    
    private static final int TXT_PASSWORD_TOP = LBL_PASSWORD_TOP;
    private static final int TXT_PASSWORD_LEFT = TXT_USER_LEFT;
    
    private static final int BTN_LOGIN_TOP = TXT_PASSWORD_TOP + TXT_PASSWORD_HEIGHT + SPACE_GAP;
    private static final int BTN_LOGIN_LEFT = TXT_PASSWORD_LEFT;
    
    private static final int BTN_EXIT_TOP = BTN_LOGIN_TOP;
    private static final int BTN_EXIT_LEFT = BTN_LOGIN_LEFT + BTN_LOGIN_WIDTH + SPACE_GAP;
    
    private static final int LBL_ERROR_TOP = BTN_LOGIN_TOP + BTN_LOGIN_HEIGHT + SPACE_GAP;
    private static final int LBL_ERROR_LEFT = 0;
    
    private JLabel m_lblUsername = null;
    private JLabel m_lblPassword = null;
    private JTextField m_txtUsername = null;
    private JTextField m_txtPassword = null;
    private JButton m_btnLogin = null;
    private JButton m_btnExit = null;
    private JLabel m_lblError = null;
    
    public LoginScreen(RenderPanel renderPanel) {
        this(renderPanel.getWidth(), renderPanel.getHeight(), renderPanel);
    }
    
    public LoginScreen(int iScreenWidth, int iScreenHeight, RenderPanel renderPanel) {
        super(iScreenWidth, iScreenHeight, renderPanel);
        initialize();
    }

    private void initialize() {
        setLayout(null);
        
        // Load background image
        try {
            m_BackgroundImage = ImageIO.read(getClass().getResource("/resources/images/login/login_background.jpg"));
        } catch (IOException ex) {
        }
        
        m_lblUsername = new JLabel();
        m_lblUsername.setText("Username:");
        m_lblUsername.setSize(LBL_USERNAME_WIDTH, LBL_USERNAME_HEIGHT);
        m_lblUsername.setLocation(LBL_USERNAME_LEFT, LBL_USERNAME_TOP);
        add(m_lblUsername);
        
        m_lblPassword = new JLabel();
        m_lblPassword.setText("Password:");
        m_lblPassword.setSize(LBL_PASSWORD_WIDTH, LBL_PASSWORD_HEIGHT);
        m_lblPassword.setLocation(LBL_PASSWORD_LEFT, LBL_PASSWORD_TOP);
        add(m_lblPassword);
        
        m_txtUsername = new JTextField();
        m_txtUsername.setSize(TXT_USERNAME_WIDTH, TXT_USERNAME_HEIGHT);
        m_txtUsername.setLocation(TXT_USER_LEFT, TXT_USER_TOP);
        add(m_txtUsername);
        
        m_txtPassword = new JTextField();
        m_txtPassword.setSize(TXT_PASSWORD_WIDTH, TXT_PASSWORD_HEIGHT);
        m_txtPassword.setLocation(TXT_PASSWORD_LEFT, TXT_PASSWORD_TOP);
        add(m_txtPassword);
        
        m_btnLogin = new JButton();
        m_btnLogin.setText("Login");
        m_btnLogin.setSize(BTN_LOGIN_WIDTH, BTN_LOGIN_HEIGHT);
        m_btnLogin.setLocation(BTN_LOGIN_LEFT, BTN_LOGIN_TOP);
        m_btnLogin.addActionListener(this);
        add(m_btnLogin);
        
        m_btnExit = new JButton();
        m_btnExit.setText("Exit");
        m_btnExit.setSize(BTN_EXIT_WIDTH, BTN_EXIT_HEIGHT);
        m_btnExit.setLocation(BTN_EXIT_LEFT, BTN_EXIT_TOP);
        m_btnExit.addActionListener(this);
        add(m_btnExit);
        
        m_lblError = new JLabel();
        m_lblError.setText("");
        m_lblError.setSize(LBL_ERROR_WIDTH, LBL_ERROR_HEIGHT);
        m_lblError.setLocation(LBL_ERROR_LEFT, LBL_ERROR_TOP);
        m_lblError.setHorizontalAlignment(SwingConstants.CENTER);
        m_lblError.setForeground(Color.RED);
        add(m_lblError);
    }
    
    @Override
    public void handlePacket(byte[] data) throws Exception {
        ByteArrayInputStream BAIS = new ByteArrayInputStream(data);
        DataInputStream DIS = new DataInputStream(BAIS);
        int packetID = DIS.readInt();

        switch (packetID) {
            case 1:
                RLoginRequestResponse receivedPacket = new RLoginRequestResponse();
                receivedPacket.fromByteArray(data);
                if (receivedPacket.getLoginStatus()) {
                    m_lblError.setText("OK");
                }
                else {
                    m_lblError.setText(Messages.LOGIN__LOGIN_FAILED);
                }
                break;
        }
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        Object source = ae.getSource();
        if(source == m_btnLogin) {
            login();
            return;
        }
        if(source == m_btnExit) {
            exit();
        }
    }
    
    private void login() {
        try {
            String strUsername = m_txtUsername.getText();
            if(strUsername == null || "".equals(strUsername)) {
                m_lblError.setText(Messages.LOGIN__BLANK_USERNAME);
                return;
            }
            
            String strPassword = m_txtPassword.getText();
            if(strPassword == null || "".equals(strPassword)) {
                m_lblError.setText(Messages.LOGIN__BLANK_PASSWORD);
                return;
            }
            m_ConnectionHandler.connectToServer(Configurations.GAME_SERVER_IP, Configurations.GAME_SERVER_PORT);
            SLoginRequest loginRequest = new SLoginRequest(strUsername, strPassword);
            m_ConnectionHandler.send(loginRequest);
        } catch (UnknownHostException e) {
            m_lblError.setText(Messages.NETWORK_ERROR);
        } catch (IOException e) {
            m_lblError.setText(Messages.NETWORK_ERROR);
        }
    }
    
    private void exit() {
        m_Game.exit();
    }

    @Override
    public void paintScreen(Graphics grphcs) {
        
    }
    /*
    @Override
    public void paintScreen(long deltaTime, Graphics grphcs) {
        
    }
    */

    @Override
    public void update(long deltaTime) {
        
    }

    @Override
    public void pause() {
        
    }

    @Override
    public void resume() {
        
    }

    @Override
    public void dispose() {
        
    }
}
